/* eslint-disable no-undef */
import { EnumUtility } from './EnumUtility'
import * as dateUtility from './DateUtil'
import { IsDate, IsEmpty, IsNumber } from './ObjectUtil' 
import moment from "moment";
function SubstituteEnum(value, enumName) {
let subval = value;
if (value !== "") {
    if (value == "*") {
        return subval = "所有"
    } else {
        let enumUtility = new EnumUtility() 
        return enumUtility.GetTitle(enumName, value)
    }
} else {
    subval = '---'
}
return subval
}

function SubStituteHolder(value) {
    if (value)
        return value
    else
        return '---'
}

function SubstituteDate(value, format) {
    let subval = value
    if (!IsDate(value)) {
        subval = dateUtility.Create(value) || dateUtility.Create2(value)
    }
    if (!IsDate(subval)) {
        subval = dateUtility.Create(subval)
    }

    if (subval == '' || subval == undefined) {
        return '---'
    } else {
        return dateUtility.ToString(subval, format, false)
    }
}

function SubstituteMobileDate(value, format) {
    let subval = value
    // if (!IsDate(value)) {
    //     // subval = dateUtility.Create(value) || dateUtility.Create2(value)
    //     subval = moment(value).toDate()
    // }
    // if (!IsDate(subval)) {
    //     subval = dateUtility.Create(subval)
    // }

    if (subval == '' || subval == undefined) {
        return '---'
    } else {
        subval = moment(value).toDate()
        return dateUtility.ToString(subval, format, false)
    }
}

function SubstituteTof(value, tof) {
    let status = tof.split(',')
    let index = parseInt(value)
    return status[index]
}

function SubstituteEllipsis(value, number) {
    if (IsEmpty(value)) {
        return '---'
    }
    if (value.length >= number) {
        let subval = value.slice(0, number - 1) + '...'
        return subval
    } else if (0 < value.length < number) {
        let subval = value
        return subval
    } else {
        return '---'
    }
}

function SubstituteNumber(value) {
    if (value === '') {
        return 0
    } else {
        return value
    }
}

function SubstituteNumberFixString(value, number) {
    if (IsEmpty(value)) {
        return 0
    }
    if (!IsNumber(value) || !IsNumber(number)) {
        return value
    }

    return value.toFixed(number)
}

function SubstituteFlowStandard(value) {
    let result = value
    if (value == undefined || value == "") {
        return result
    }
    let val = parseFloat(value)
    if (val >= 1024) {
        val = val / 1024.0
        result = Math.floor(val * 100) / 100 + "GB"
    } else if (val > 0) {
        result = val + "MB"
    } else {
        result = val + ""
    }
    return result
}


function SubstitueTranFlow(value, number) {
    if (IsEmpty(value)) {
        return ''
    }
    try {
        let val = Number.parseFloat(value)
        if (val >= 1024) {
            return Math.floor(val / 1024.0 * 100) / 100 + 'G'
        } else if (val > 0) {
            return val + 'M'
        } else {
            return val
        }
    } catch (ex) {
        return value
    }
}

function SubstituteList(value, list) {
    if(list.length < 1){
      return value;
    }

    var val = value;
    list.forEach(function(item){
      if(item.value == value){
        val = item.title;
        return;
      }
    });

    return val;
}

function SubstituteSubString(value, len) {
    if(value.length < len || len < 1)
        return value;

    return value.substring(0, len);
}

export {
    SubstituteEnum,
    SubStituteHolder,
    SubstituteDate,
    SubstituteTof,
    SubstituteEllipsis,
    SubstituteNumber,
    SubstituteFlowStandard,
    SubstituteNumberFixString,
    SubstituteMobileDate,
    SubstitueTranFlow,
    SubstituteList,
    SubstituteSubString
}